package com.framework.admin.service.impl;

import com.framework.admin.repository.SysDictRepository;
import com.google.common.collect.Lists;
import com.framework.admin.mapper.DictMapper;
import com.framework.admin.model.SysDict;
import com.framework.admin.service.IDictService;
import com.framework.common.exception.BusinessException;
import com.framework.common.util.Constant;
import com.framework.common.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class DictService implements IDictService {
    @Autowired
    SysDictRepository sysDictRepository;
    @Autowired
    DictMapper dictMapper;

    @Override
    public Page<SysDict> getDictList(SysDict dict, Pageable pageable) {
        return dictMapper.findDicts(dict, pageable);
    }

    @Override
    public void save(SysDict dict) {
        //编辑
        if (StringUtils.isNotEmpty(dict.getId())) {
            SysDict oldDbEntity = sysDictRepository.findOne(dict.getId());
            //修改了code
            if (!dict.getCode().equals(oldDbEntity.getCode())) {
                SysDict newDict;
                if (Constant.ROOT.equals(dict.getPcode())) {
                    newDict = sysDictRepository.findTopByCode(dict.getCode());
                } else {
                    newDict = sysDictRepository.findTopByPcodeAndCode(dict.getPcode(), dict.getCode());
                }
                if (newDict != null) {
                    throw new BusinessException("字典code已存在,请勿重复添加!");
                }
                List<SysDict> childDicts = findByPcode(oldDbEntity.getCode());
                if (StringUtils.isNotEmpty(childDicts)) {
                    //同步修改子类的父级code
                    for (SysDict childDict : childDicts) {
                        childDict.setPcode(dict.getCode());
                        sysDictRepository.save(childDict);
                    }
                }
            }
            sysDictRepository.save(dict);
        } else {
            SysDict newDict;
            //新增
            if (Constant.ROOT.equals(dict.getPcode())) {
                newDict = sysDictRepository.findTopByCode(dict.getCode());
            } else {
                newDict = sysDictRepository.findTopByPcodeAndCode(dict.getPcode(), dict.getCode());
            }
            if (newDict != null) {
                throw new BusinessException("字典code已存在,请勿重复添加!");
            }
            sysDictRepository.save(dict);
        }
    }

    @Override
    public void delete(List<String> ids) {
        List<String> allIds = Lists.newArrayList();
        allIds.addAll(ids);
        for (String id : ids) {
            List<SysDict> dicts = findByPcode(sysDictRepository.findOne(id).getCode());
            if (StringUtils.isNotEmpty(dicts)) {
                List<String> childIds = dicts.stream().map(SysDict::getId).collect(Collectors.toList());
                allIds.addAll(childIds);
            }
        }
        String[] arr = new String[allIds.size()];
        sysDictRepository.deleteById(allIds.toArray(arr));
    }

    @Override
    public List<SysDict> findByPcode(String code) {
        return sysDictRepository.findByPcodeOrderBySort(code);
    }

}
